<script setup>
import { reactive, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app';
import { findOperationInfoList, deliverWorkOrder } from '@/api/system'

const registerForm = ref()

const resetParams = reactive({
    workOrderId: null,
    management: null,
    managementName: null,
    acceptor: null,
    acceptorName: null,
})

const customRules = ref({
  acceptor: {
    rules: [{ required: true, errorMessage: '受理人员不能为空' }]
  }
})

//根据当前管理单位获取人员列表
const ACCEPTOR_LIST = ref([])

onLoad((options) => {
  console.log("options", options);
  resetParams.workOrderId = options.id;
  resetParams.management = options.acceptorManagement;
  resetParams.managementName = options.acceptorManagementName;
  resetParams.acceptorName = options.acceptorName;
  getList(resetParams.management)
});

const getList = async (management) => {
  const acceptorRes = await findOperationInfoList({
    management: management
  });
  console.log("acceptorRes.data", acceptorRes.data);//数组
  if (acceptorRes.code === 200) {
    ACCEPTOR_LIST.value = acceptorRes.data || [];
    
    ACCEPTOR_LIST.value.map((item) => {
      item.text = item.operationName;
      item.value = item.id;
      return item;
    });
  }
  // console.log("ACCEPTOR_LIST", ACCEPTOR_LIST);
}

/**
 * 派单操作
 */
const handleResetPassword = async () => {
  registerForm.value
    .validate()
    .then(async () => {
      const { code, msg } = await deliverWorkOrder({
        workOrderId: resetParams.workOrderId,
        receiveUserId: resetParams.acceptor
      })
      if (code == 200) {
        uni.showModal({
          title: '提示',
          content: msg,
          showCancel: false,
          success: (res) => {
            if (res.confirm) {
              uni.reLaunch({
                url: '/homePages/workOrder/index'
              })
            }
          }
        })
        return
      }

      uni.showModal({ title: '提示', content: msg })
    })
    .catch((err) => {
      console.log('err', err)
    })
}
</script>
<template>
  <view class="example">
    <uni-forms
      label-position="left"
      label-width="80"
      :modelValue="resetParams"
      :rules="customRules"
      ref="registerForm"
      class="form"
    >
      <uni-forms-item label="管理单位" required>
        <uni-easyinput
          placeholder="管理单位"
          v-model="resetParams.managementName"
          disabled
        />
      </uni-forms-item>
      <uni-forms-item label="受理人员" required name="acceptor">
        <uni-data-select
          :localdata="ACCEPTOR_LIST"
          placeholder="请选择受理人员"
          v-model="resetParams.acceptor"
        />
      </uni-forms-item>
    </uni-forms>
    <button type="primary" @click="handleResetPassword">确认转单</button>
  </view>
</template>

<style lang="scss" scoped>
.form {
  margin-bottom: 22px;
}
.example {
  padding: 15px;
  background-color: #fff;
}
.captchaBtn {
  line-height: 35px;
  font-size: 14px;
  height: 35px;
  margin-left: 8px;
}

button[disabled] {
    color: #fff !important;
    background: #71b2f9 !important;
    border-color: #28aed7 !important;
}
</style>
